package fr.inria.phoenix.scenario.bwsnfirefighting.framework.distributed.light;

import java.util.HashMap;

import fr.inria.diagen.core.ServiceConfiguration;
import fr.inria.diagen.core.exception.DiaGenException;
import fr.inria.diagen.core.exception.InvocationException;
import fr.inria.diagen.core.network.RemoteServiceInfo;
import fr.inria.diagen.core.service.local.Service;

import java.lang.Boolean;
import fr.inria.phoenix.scenario.bwsnfirefighting.framework.datatype.DeviceStatus;

import fr.inria.diagen.core.module.command.ICommandListener;

/**
 * Define a Light device which can be activated in case of alarm
 *
 * @[50,0]
 * device Light {
 *   attribute activated as Boolean;
 *   source status as DeviceStatus;
 *   action Activate;
 * }
 */
public abstract class Light extends Service implements ICommandListener {

  /**
     * @param serviceConfiguration
     */
    public Light(ServiceConfiguration serviceConfiguration , java.lang.Boolean activated) {
      super("Light", serviceConfiguration, GeneratedFramework.getInstance());
      setActivated(activated);
    }

    @Override
    public final HashMap<String, Object> getAttributes() {
        HashMap<String, Object> attributes = new HashMap<String, Object>();
    
        attributes.put("activated",activated);

        return attributes;
    }
    
    private java.lang.Boolean activated;

    /**
     * @[51,11]
     * attribute activated as Boolean;
     * @param newActivatedValue
     */
    protected void setActivated(java.lang.Boolean newActivatedValue) {
      if (activated != newActivatedValue) {
        activated = newActivatedValue;
        updateAttributes();
      }
    }

    /**
     * @[51,11]
     * attribute activated as Boolean;
     * @return
     */  
    public java.lang.Boolean getActivated() {
      return activated;
    }

	protected final void _postInitialize() {postInitialize();}
	
	/**
	 *
	 */
	protected void postInitialize() {}

    /**
     * 
     */
    protected abstract void deactivate() throws DiaGenException;

    /**
     * 
     */
    protected abstract void activate() throws DiaGenException;

  
    public final Object commandReceived(RemoteServiceInfo rsi, String methodName, Object... args) throws DiaGenException {
    if (methodName.equals("deactivate")) {
        deactivate();
        return null;
      } else     if (methodName.equals("activate")) {
        activate();
        return null;
      }
  else  

    if (methodName.equals("getStatus")) {
      return getStatus();
    }
    
      throw new InvocationException("Undefined method name " + methodName);
    }
  

    private fr.inria.phoenix.scenario.bwsnfirefighting.framework.datatype.DeviceStatus status;
    /**
     * @[53,1]
     * source status as DeviceStatus;
     */  
    protected void setStatus(fr.inria.phoenix.scenario.bwsnfirefighting.framework.datatype.DeviceStatus newStatusValue ) {
      status = newStatusValue;
      getServer().publish("status", newStatusValue); 
    }

    /**
     * @[53,1]
     * source status as DeviceStatus;
     */
    protected fr.inria.phoenix.scenario.bwsnfirefighting.framework.datatype.DeviceStatus getStatus() {
      return status;    }
 
}